package com.dev;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.app.Application;
class ExtraKey extends LinearLayout implements OnClickListener{
	key tab,k0,k1,k2,k3,k4,k5,k6,k7,k8,k9,ka;
	boolean capsLock=false;
	final ColorDrawable pressed=new ColorDrawable(0x66666655);
	Drawable backgroundSaved;
	EditText destination;
	MainActivity ma;
	ExtraKey(MainActivity ma){
		super(ma);
		this.ma=ma;
		setBackgroundColor(0xffffffff);
		setOrientation(HORIZONTAL);
		tab=new TabKey();
		//k0=new TagKey();
		k1=new CapsLockKey();
		k2=new key("<");
		k3=new key(">");
		k4=new key("{");
		k5=new key("}");
		k6=new key("\\");
		k7=new key("ˌ");
		k8=new key("·");
		//k9=new key("※");
		//ka=new key("°");
		LayoutParams klp=new LayoutParams(0,-1,1);
		//addView(k1,klp);
		addView(k2,klp);
		addView(k3,klp);
		addView(k4,klp);
		addView(k5,klp);
		addView(k6,klp);
		addView(k7,klp);
		addView(k8,klp);
		//addView(k9,klp);
	}
	@Override
	public void onClick(View v){
	}
	private final class TabKey extends key{
		public TabKey(){
			super("↹");
			s="\t";
		}
	}
	public final class CapsLockKey extends key{
		Paint p=new Paint();
		int cl,ct,cr,cb,thin,pillar;
		public CapsLockKey(){
			super("");
			p.setStrokeWidth(3);
		}
		@Override
		protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
			setPadding(0,getMeasuredHeight()/5,0,getMeasuredHeight()/5);
			int spaceHor=getMeasuredWidth()-getPaddingLeft()-getPaddingRight();
			int spaceVer=getMeasuredHeight()-getPaddingTop()-getPaddingBottom();
			int widthWillset=spaceVer*3/4;
			if(widthWillset>spaceHor){
				cl=getPaddingLeft();
				cr=getMeasuredWidth()-getPaddingRight();
				int ch=(cr-cl)*4/3;
				ct=spaceVer-ch;
				ct/=2;
				ct+=getPaddingTop();
				cb=ct+ch;
				thin=ch/4;
			}else{
				//log.alert("23333");
				ct=getPaddingTop();
				cb=getMeasuredHeight()-getPaddingBottom();
				int cw=(cb-ct)*3/4;
				cl=spaceHor-cw;
				cl/=2;
				cl+=getPaddingLeft();
				cr=cl+cw;
				thin=cw/4;
			}
			pillar=(cb-ct)/2+ct;
		}
		@Override
		public void onClick(View v) {
			//super.onClick(v);
			capsLock=!capsLock;
			k1.invalidate();
		}
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			Path path=new Path();
			//canvas.drawRect(cl,ct,cr,cb,p);
			if(capsLock){
				p.setStyle(Paint.Style.FILL);
			}else{
				p.setStyle(Paint.Style.STROKE);
			}
			path.moveTo(cl+thin,cb);
			path.lineTo(cl+thin,pillar);
			path.lineTo(cl,pillar);
			path.lineTo(cl+(cr-cl)/2,ct);
			path.lineTo(cr,pillar);
			path.lineTo(cr-thin,pillar);
			path.lineTo(cr-thin,cb);
			path.close();
			canvas.drawPath(path,p);
		}
	}
	private class key extends TextView implements OnClickListener{
		String s;
		public key(String s){
			super(ma);
			this.s=s;
			setLayoutParams(new LinearLayout.LayoutParams(0,-1,1));
			setGravity(Gravity.CENTER);
			setText(s);
			setTextSize(TypedValue.COMPLEX_UNIT_DIP,14);
			setTypeface(Typeface.MONOSPACE,Typeface.BOLD);
			setTextColor(0xff000000);
			setOnClickListener(this);
			lenlim=(int)(15*ma.dp);
		}
		Point touch_down=new Point();
		Point touch_downRaw=new Point();
		long touch_timeSaved;
		boolean fingerHorizontallyMoved=false;
		boolean fingerVerticalMoved=false;
		boolean fingerMoved;
		int lenlim;
		int selectionStartSaved;
		int selectionEndSaved;
		int linear2lp_heightSaved;
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			int ex=(int)event.getX();
			int ey=(int)event.getY();
			switch(event.getAction()){
				case MotionEvent.ACTION_DOWN:
					touch_down.x=ex;
					touch_down.y=ey;
					touch_downRaw.x=(int)event.getRawX();
					touch_downRaw.y=(int)event.getRawY();
					backgroundSaved=getBackground();
					selectionStartSaved=destination.getSelectionStart();
					selectionEndSaved=destination.getSelectionEnd();
					fingerHorizontallyMoved=false;
					fingerVerticalMoved=false;
					fingerMoved=false;
					setBackgroundDrawable(pressed);
					//linear2lp_heightSaved=linear2lp.height;
					break;
				case MotionEvent.ACTION_MOVE:
					if(fingerMoved){
						if(fingerHorizontallyMoved){
							int xMoved=(int)event.getX()-touch_down.x;
							int selectionEndWill=selectionEndSaved+xMoved/lenlim;
							if(selectionEndWill>=0){
								if(selectionEndWill<=destination.length()){
									destination.setSelection(destination.getSelectionStart(),selectionEndWill);
								}else{
									destination.setSelection(destination.getSelectionStart(),destination.length());
								}
							}else{
								destination.setSelection(destination.getSelectionStart(),0);
							}
						}
					}else{
						int xMoved=(int)event.getX()-touch_down.x;
						int yMoved=(int)event.getY()-touch_down.y;
						int xMoved_abs=Math.abs(xMoved);
						int yMoved_abs=Math.abs(yMoved);
						if(xMoved_abs>yMoved_abs){
							if(xMoved_abs>5||yMoved_abs>5){
								fingerHorizontallyMoved=true;
							}else if(xMoved_abs<yMoved_abs){
								fingerVerticalMoved=true;
							}
							fingerMoved=true;
						}
					}
					break;
				case MotionEvent.ACTION_UP:
					setBackgroundDrawable(null);
					break;
				case MotionEvent.ACTION_CANCEL:
					setBackgroundColor(0x00ffffff);
					if(ey>=0&&ey<getHeight()){
						if(ex>=getWidth()){
						}else if(ex<0){
						}
					}
					break;
			}
			super.onTouchEvent(event);
			return true;
		}
		@Override
		public void onClick(View p1) {
			if(destination!=null){
				int a=destination.getSelectionStart();
				int b=destination.getSelectionEnd();
				destination.getEditableText().replace(a,b,s);
			}
		}
	}
}
